<?php
/* ###############################################################################
 *
 * $Id: html.php 67 2015-02-18 18:00:51Z expertsoftware.i@gmail.com $
 *
 * システム名
 *   PHP Web フレームワーク [Ponzu]
 *
 * 機能
 *   HTML ライブラリ
 *
 * バージョン
 *   0.0.0_0
 *
 * ライセンス
 *   LGPL v2.0
 *
 * 履歴
 *   2007/07/01 K.Sonohara
 *     新規作成。
 *
 * ###############################################################################*/

/**
 * 改行
 * @access public
 */
function lnHTML($always=false) {
  if (SITE_DEBUG || $always) {
    echo "\n";
  }
}

/**
 * インデント
 * @access public
 */
function indentHTML($count, $always=false) {
  if (SITE_DEBUG || $always) {
    for ($i = 0; $i < $count; $i++) {
      echo "  ";
    }
  }
}
function escapeHTML($text) {
	$t = $text;
	$t = str_replace("&", "&amp;", $t);
	$t = str_replace("<", "&lt;", $t);
	$t = str_replace(">", "&gt;", $t);
	$t = str_replace('"', "&quot;", $t);

	return $t;
}

/**
 * HTMLタグ出力
 * @access public
 */
function printHTML($text, $index=2, $ln_always=false) {
  indentHTML($index);
  echo $text;
  lnHTML($ln_always);
}

function printMeta($name, $content, $equiv=null) {
  $t = '<meta ';
  if ($name) {
    $t = $t . 'name="'. $name . '" ';
  }
  if ($content) {
    $t = $t . 'content="'. $content . '" ';

  }
  if ($equiv) {
    $t = $t . 'http-equiv="'. $equiv . '" ';

  }
  $t = $t . '/>';

  printHTML($t, 2, true);
}

function printLink($href, $rel=null, $type=null, $title=null) {
  $t = '<link ';
  if ($href) {
    $t = $t . 'href="'. $href . '" ';
  }
  if ($rel) {
    $t = $t . 'rel="'. $rel . '" ';

  }
  if ($type) {
    $t = $t . 'type="'. $type . '" ';

  }
  if ($title) {
    $t = $t . 'title="'. $title . '" ';

  }
  $t = $t . '/>';

  printHTML($t, 2, true);
}

function printJS($href, $dir=true) {
  $v = $href;
  if ($dir) {
    if (($v != "") and ($v[0] != "/")) {
      if (strpos($href, "http://") . strpos($href, "https://") == "") {
        $v = SITE_URL.'js/'.$href;
      }
    }
  }

  $t = '<script src="'. $v . '" type="text/javascript" charset="'.SITE_ENCODE.'"> </script>';

  printHTML($t, 2, true);
}
function beginScript() {
  printHTML('<script type="text/javascript">', 2, true);
}
function endScript() {
  printHTML('</script>', 2, true);
}
function printScript($text) {
  $t = '<script type="text/javascript">' . $text . '</script>';

  printHTML($t, 2, true);
}



/**
 * XML出力
 * @access public
 */
function beginXML() {
  if (!HTML5) {
    printHTML('<?xml version="1.0" encoding="'.SITE_ENCODE.'" standalone="no"?>', 0, true);
    lnHTML(true);
  }
}

/**
 * XML出力
 * @access public
 */
function endXML() {
  lnHTML(true);
}

/**
 * HTML出力
 * @access public
 */
function beginHTML() {
  header( 'Content-Type: text/html; charset='.SITE_ENCODE);

  beginXML();

  printHTML('<!DOCTYPE html>', 0, true);
  printHTML('<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">', 0, true);
}

/**
 * HTML出力
 * @access public
 */
function endHTML() {
  printHTML('</html>', 0, true);

  endXML();
}

function beginHeader() {
  printHTML('<head>', 1, true);
}

function endHeader() {
  printHTML('</head>', 1, true);
}

function doHeader() {
  beginHeader();
  // 文字コード
  printHTML('<meta charset="' . SITE_ENCODE . '" />', 2, true);
  printMeta(null, 'text/html; charset='.SITE_ENCODE, "Content-Type");
  if (!HTML5) {
    printMeta(null, "ja", "content-language");
  }

  // タイトル
  $title = SITE_TITLE;
  if (defined("PAGE_TITLE")) {
    $title = PAGE_TITLE . " - " . $title;
  }
  printHTML('<title>' . $title . '</title>', 2, true);

  // ヘッダー情報
  if (!HTML5) {
    printMeta(null, "text/css", "Content-Style-Type");
  }
  printMeta("author", SITE_AUTHER);
  $description = SITE_DESCRIPTION;
  if (defined("PAGE_DESCRIPTION")) {
    $description = PAGE_DESCRIPTION;
  }
  printMeta("description", $description);
  $robots = SITE_ROBOTS;
  if (defined("PAGE_ROBOTS")) {
    $robots = PAGE_ROBOTS;
  }
  printMeta("robots", $robots);
  $keywords = SITE_KEYWORDS;
  if (defined("PAGE_KEYWORDS")) {
    $keywords = $keywords . PAGE_KEYWORDS;
  }
  printMeta("keywords", $keywords);

  // 表示設定
  if (defined("SITE_VIEWPORT")) {
    printMeta("viewport", SITE_VIEWPORT);
  }

  // LESS
  global $less;
  foreach ($less as $value) {
    printLink(SITE_URL.'less/'.$value, "stylesheet/less", "text/css");
  }

  // CSS
  global $css;
  foreach ($css as $value) {
    printLink(SITE_URL.'css/'.$value, "stylesheet", "text/css");
  }

  // JavaScript
  global $script;
  if (count($script) > 0) {
    beginScript();
    foreach ($script as $value) {
      print($value);
    }
    endScript();
  }

  // Bootstrap
  if (SITE_BOOTSTRAP) {
    printHTML('<!--[if lt IE 9]>', 2, true);
    printHTML('<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>', 2, true);
    printHTML('<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>', 2, true);
    printHTML('<![endif]-->', 2, true);

    printMeta(null, "IE=edge", "X-UA-Compatible");
  }

  // JavaScript
  global $js;
  foreach ($js as $value) {
    printJS($value);
  }

  // RSS
  if (defined("PAGE_RSS")) {
    printLink(PAGE_RSS, "alternate", "application/rss+xml", "RSS 1.0");
  } else if (defined("SITE_RSS")) {
    printLink(SITE_RSS, "alternate", "application/rss+xml", "RSS 1.0");
  }

  // favicon
  printLink(SITE_ICON, "shortcut icon");
  printLink(SITE_ICON_APPLE, "apple-touch-icon-precomposed");

  endHeader();
}

function beginBody() {
  // mikan.js
  if (SITE_MIKANJS) {
    printHTML('<body onload="mikan.page.load();" onunload="mikan.page.unload();">', 1, true);
  } else {
    printHTML('<body>', 1, true);
  }
}

function endBody() {
  printHTML('</body>', 1, true);
}
function doBody($printBody="SITE_HTML") {
  beginBody();

  if (($printBody != null) && is_callable($printBody)) {
    call_user_func($printBody);
  }

  // JavaScript
  global $jss;
  foreach ($jss as $value) {
    printJS($value);
  }

  global $scripts;
  if (count($scripts) > 0) {
    beginScript();
    foreach ($scripts as $value) {
      print($value);
    }
    endScript();
  }

  endBody();
}

function doHTML($printBody="SITE_HTML") {
  beginHTML();
  doHeader();
  doBody($printBody);
  endHTML();
}

?>
